<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Console\Controller;

/**
 * 后台联系人管理控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */

class ContactsController extends ConsoleController {

	public function _initialize(){
        parent::_initialize();
		$this->assign('currentGroup',$this->getMenuCurrent(self::$nodes));
    }
    static protected $allow = array( 'tree');
    /**
     * 左侧导航节点定义
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    static protected $nodes = array(

        /* 导航栏目设置 */
        array( 'title' => '全部通讯录', 'url' => 'Contacts/index', 'group' => '通讯簿',
            'operator'=>array(
                array('title'=>'编辑','url'=>'Postition/edit','tip'=>'编辑和保存栏目联系人'),
            	array('title'=>'新增','url'=>'Category/add','tip'=>'新增栏目联系人'),
            	array('title'=>'删除','url'=>'Category/remove','tip'=>'删除栏目联系人'),
            ),
        ),
    );

    /**
     * 联系人管理列表
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
       
		/* 查询条件初始化 */
    	$map = array();
        $map  = array('A.status' => 1);
        if(isset($_GET['name'])){
        	$map['name']  = array('like', '%'.(string)I('name').'%');
        }
		$Contacts = M('Member')->alias('A')->join('onethink_department B ON A.department_id = B.id');
		$list = $this->lists($Contacts, $map,$order='A.uid',$base = array(),'A.*,B.id,B.title department');
        $this->assign('list', $list);
        $this->main_title = '通讯录管理';
        $this->meta_title = '联系人管理';
        $this->display();
    }

    /**
     * 显示联系人树，仅支持内部调
     * @param  array $tree 联系人树
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function tree($tree = null){	
        C('_SYS_GET_CATEGORY_TREE_') || $this->_empty();		
        $this->assign('tree', $tree);
        $this->display('tree');
    }

    /* 编辑联系人 
     * @author sunguide <sunguide@qq.com>
     */
    public function edit($id = null, $pid = 0){
        $Contacts = D('Contacts');

        if(IS_POST){ //提交表单
            if(false !== $Contacts->update()){
                $this->success('编辑成功！', U('index'));
            } else {
                $error = $Contacts->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {

            /* 获取联系人信息 */
            $info = $id ? $Contacts->info($id) : '';

            $this->assign('info',       $info);
            $this->assign('category',   $cate);
            $this->meta_title = '编辑联系人';
            $this->display();
        }
    }
    /* 新增联系人 
     * @author sunguide <sunguide@qq.com>
     */
    public function add($pid = 0){
        $Contacts = D('Contacts');

        if(IS_POST){ //提交表单
            if(false !== $Contacts->update()){
				$in = $Contacts->getLastSql();
                $this->success('新增成功！', U('index'));
            } else {
                $error = $Contacts->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {

            $cate = array();
            if($pid){
                /* 获取上级联系人信息 */
                $cate = $Contacts->info($pid, 'id,name,title,status');
                if(!($cate && 1 == $cate['status'])){
                    $this->error('指定的上级联系人不存在或被禁用！');
                }
            }
            /* 获取联系人信息 */
            $this->assign('position', $cate);
            $this->meta_title = '新增联系人';
            $this->display('edit');
        }
    }

    /**
     * 删除一个联系人
     * @author sunguide <sunguide@qq.com>
     */
    public function remove(){
        $cate_id = I('id');
        if(empty($cate_id)){
            $this->error('参数错误!');
        }else{
			if(is_array($cate_id)){
				//删除该组联系人信息
				foreach($cate_id as $id){
					$data = array(
						'uid'	 =>$id,
						'status' => 0
					);
					$res = M('Member')->save($data);
				}
			}else{
				//删除该联系人信息
				$data = array(
					'uid'	 =>$cate_id,
					'status' => 0
				);
				$res = M('Member')->save($data);
			}
			
			if($res !== false){
				$this->success('删除联系人成功！');
			}else{
				$this->error('删除联系人失败！');
			}
		}
        
    }
}
